package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Contact;
import domain.DAOContact;

/**
 * Servlet implementation class FindContact
 */
public class FindContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DAOContact dao=DAOContact.getInstance();	
		Contact c;
		List<Contact> contacts = null;
		
		
			String idString=request.getParameter("id");
			
			if(idString!=null){
					long id=Long.parseLong(idString);
					c=dao.getContact(id);
					request.setAttribute("contact", c);
					getServletContext().getRequestDispatcher("/displayContact.jsp").forward(request, response);
			}else{
				String lastName=request.getParameter("lastname");
				if(lastName!=null){
					contacts=dao.getContactByLastName(lastName);
					System.out.println(contacts.size());
				}else {
					String firstName=request.getParameter("firstname");
					if(firstName!=null){
						
						contacts=dao.getContactByLastName(firstName);
						System.out.println(contacts.size());
					}else{
						String email=request.getParameter("email");
						if(email!=null){
							contacts=dao.getContactByLastName(email);
						}	
					}
				}
				request.setAttribute("contacts", contacts);				
				getServletContext().getRequestDispatcher("/displayContacts.jsp").forward(request, response);
			} 
			

			
			
			
			
	}

}
